Ext.define('PJ.view.trainingmanagement.SelectUnitFrame', {
    extend: 'AM.default.DefaultWindowForm',
    alias: ['widget.selectUnitFrame'],
    requires: ['AM.default.DateTime', 
               'AM.default.DefaultDateField',
               'AM.default.jsonp.DefaultSubmitJsonp'],
    
    modal: true,// 开启不允许点击其他组件
    frame: true,
    notEdit: true,
    width: 700,
    height: 550,
    layout: 'form',
    //autoScroll: true,
    overflowX: 'hidden',
    overflowY: 'auto',
    initComponent: function(){
        var me = this;
        var questionnaireManageList = Ext.getCmp("celaQuestionnaireManageList");
        me.items = [{
            defaults: {
                labelAlign: 'right',
                labelWidth: 85
            },
            border: false,
            xtype: "container",
            width: 660,
            layout: 'column',
            items: [{
            	xtype:'fieldset',
                columnWidth: 1,
                title: '单选题',
                collapsible: true,
                items: [{
                	xtype: 'celaIndividualChoiceList',
                	id: 'celaIndividualChoiceList',
                	itemId: 'celaIndividualChoiceList',
                	margin: '10 0 10 0',
                	listeners: {
                		selectionchange: function(selModel, selected){
                			this.down('#' + this.oldId + "Delete").setDisabled(selected.length === 0);
                		},
                		afterrender: function(){
                			this.loadMask = new Ext.LoadMask(this, {
                                msg: "正在加载中,请稍后...."
                            });
                		},
                		viewready: function(){
                    		this.addDocked([{
                                xtype: 'toolbar',
                                dock: 'top',
                                displayInfo: true,
                                items: [this.addButton, "-", {
                                	text: '删除',
                                	disabled: true,
                                    itemId: this.oldId + "Delete",
                                    iconCls: 'deleteIco',
                                    handler: function(button){
                                        button.up("grid").onDeleteClick(button);
                                    }
                                }, "-", {
                                    text: '从题库选取题目',
                                    iconCls: 'setExamPaperIcon',
                                    itemId: this.oldId + "Selectquestions",
                                    disabled: false,
                                    scope: this,
                                    handler: this.onSelectquestionsClick
                                }, "->", this.countSelectPanel]
                            }]);
                    		var store = this.store;
                            var newParams = {
                            		questionnaireId:this.up("window").questionnaireId,
                            		questionType:"danxt",
                            		page: -1
                            };
                            store.proxy.extraParams = {};
                            // 4.0强制更改store参数写法
                            var params = store.proxy.extraParams;
                            Ext.apply(params, newParams);
                            store.isLoad=true;
                            store.load();
                    		// 触发权限过滤事件
                    		this.fireEvent("permission", this);
                    	}
                	}
                }]
            },
            {
            	xtype:'fieldset',
                columnWidth: 1,
                title: '多选题',
                collapsible: true,
                items: [{
                	xtype: 'celaMultipleChoiceList',
                	id: 'celaMultipleChoiceList',
                	itemId: 'celaMultipleChoiceList',
                	margin: '10 0 10 0',
                	listeners: {
                		selectionchange: function(selModel, selected){
                			this.down('#' + this.oldId + "Delete").setDisabled(selected.length === 0);
                		},
                		afterrender: function(){
                			this.loadMask = new Ext.LoadMask(this, {
                                msg: "正在加载中,请稍后...."
                            });
                		},
                		viewready: function(){
                    		this.addDocked([{
                                xtype: 'toolbar',
                                dock: 'top',
                                displayInfo: true,
                                items: [this.addButton, "-", {
                                	text: '删除',
                                	disabled: true,
                                    itemId: this.oldId + "Delete",
                                    iconCls: 'deleteIco',
                                    handler: function(button){
                                        button.up("grid").onDeleteClick(button);
                                    }
                                }, "-",  {
                                    text: '从题库选取题目',
                                    iconCls: 'setExamPaperIcon',
                                    itemId: this.oldId + "Selectquestions",
                                    disabled: false,
                                    scope: this,
                                    handler: this.onSelectquestionsClick
                                }, "->", this.countSelectPanel]
                            }]);
                    		var store = this.store;
                            var newParams = {
                            		questionnaireId:this.up("window").questionnaireId,
                            		questionType:"duoxt",
                            		page: -1
                            };
                            store.proxy.extraParams = {};
                            // 4.0强制更改store参数写法
                            var params = store.proxy.extraParams;
                            Ext.apply(params, newParams);
                            store.isLoad=true;
                            store.load();
                    		// 触发权限过滤事件
                    		this.fireEvent("permission", this);
                    	}
                	}
                }]
            },
            {
            	xtype:'fieldset',
                columnWidth: 1,
                title: '问答题',
                collapsible: true,
                items: [{
                	xtype: 'celaShortAnswerList',
                	id: 'celaShortAnswerList',
                	itemId: 'celaShortAnswerList',
                	margin: '10 0 0 0',
                	listeners: {
                		selectionchange: function(selModel, selected){
                			this.down('#' + this.oldId + "Delete").setDisabled(selected.length === 0);
                		},
                		afterrender: function(){
                			this.loadMask = new Ext.LoadMask(this, {
                                msg: "正在加载中,请稍后...."
                            });
                		},
                		viewready: function(){
                    		this.addDocked([{
                                xtype: 'toolbar',
                                dock: 'top',
                                displayInfo: true,
                                items: [this.addButton, {
                                	text: '删除',
                                	disabled: true,
                                    itemId: this.oldId + "Delete",
                                    iconCls: 'deleteIco',
                                    handler: function(button){
                                        button.up("grid").onDeleteClick(button);
                                    }
                                }, "-", {
                                    text: '从题库选取题目',
                                    iconCls: 'setExamPaperIcon',
                                    itemId: this.oldId + "Selectquestions",
                                    disabled: false,
                                    scope: this,
                                    handler: this.onSelectquestionsClick
                                }, "->", this.countSelectPanel]
                            }]);
                    		var store = this.store;
                            var newParams = {
                            		questionnaireId:this.up("window").questionnaireId,
                            		questionType:"wendt",
                            		page: -1
                            };
                            store.proxy.extraParams = {};
                            // 4.0强制更改store参数写法
                            var params = store.proxy.extraParams;
                            Ext.apply(params, newParams);
                            store.isLoad=true;
                            store.load();
                    		// 触发权限过滤事件
                    		this.fireEvent("permission", this);
                    	}
                	}
                }]
            }]
        }];
        this.buttons = [{
            text: '关闭',
            scope: this,
            handler: this.close
        }];
        this.callParent(arguments);
    },
    sub: function(){
        var win = this;
        var form = win.down('form');
        var baseForm = form.getForm();
        if (!baseForm.isValid()) {
            return;
        }
        var individualChoiceList = Ext.getCmp("celaIndividualChoiceList");
        var multipleChoiceList = Ext.getCmp("celaMultipleChoiceList");
        var shortAnswerList = Ext.getCmp("celaShortAnswerList");
        
        var individualChoiceListData = individualChoiceList.getStore().data.items;
        var multipleChoiceListData = multipleChoiceList.getStore().data.items;
        var shortAnswerListData = shortAnswerList.getStore().data.items;
        
        var individualList = new Array();
        var multipleList = new Array();
        var answerList = new Array();
        
        for(var i = 0; i < individualChoiceListData.length; i++){
        	individualList.push(individualChoiceListData[i].data);
        }
        for(var i = 0; i < multipleChoiceListData.length; i++){
        	multipleList.push(multipleChoiceListData[i].data);
        }
        for(var i = 0; i < shortAnswerListData.length; i++){
        	answerList.push(shortAnswerListData[i].data);
        }
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	form.getForm().doAction('submitJsonp',{
         	     url: questionnaireManageList.store.setSurveyUrl,
         	     params: {
         	    	questionnaireId: win.questionnaireId,
                	individualChoiceList: Ext.encode(individualList),
                	multipleChoiceList: Ext.encode(multipleList),
                	shortAnswerList: Ext.encode(answerList)
         	     },
         	     success: function(f, action){
         	               if (action.result.success) {
         	            	  Ext.Msg.alert('消息', '操作成功！', function(){
                                  var gridPanel = questionnaireManageList;
                                  var store = gridPanel.getStore();
                                  store.load();
                                  win.close();
                              });
         	               }
         	              },
         	     failure: function(f, action){
         	                if (action.result) {
         	                	Ext.Msg.alert('错误', action.result.msg);
         	                }else {
         	                	Ext.Msg.alert('错误', action.result.msg);
         	                }
         	              }
         	     });
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	win.submit({
                url: questionnaireManageList.store.setSurveyUrl,
                params: {
                	questionnaireId: win.questionnaireId,
                	individualChoiceList: Ext.encode(individualList),
                	multipleChoiceList: Ext.encode(multipleList),
                	shortAnswerList: Ext.encode(answerList)
                },
                success: function(f, action){
                    if (action.result.success) {
                        Ext.Msg.alert('消息', '操作成功！', function(){
                            var gridPanel = questionnaireManageList;
                            var store = gridPanel.getStore();
                            store.load();
                            win.close();
                        });
                    }
                },
                failure: function(f, action){
                    if (action.result) {
                        Ext.Msg.alert('错误', action.result.msg);
                    }
                    else {
                        Ext.Msg.alert('错误', action.result.msg);
                    }
                }
            });
        }
    }
});
